package com.yzcrm.erp.crm.service;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.TxSerializable;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.common.constant.BaseConstant;
import com.yzcrm.erp.crm.common.QuotationCombEnum;
import com.yzcrm.erp.crm.entity.CrmQuotationItem;
import com.yzcrm.utils.AuthUtil;
import com.yzcrm.utils.BaseUtil;
import com.yzcrm.utils.R;
import org.apache.poi.ss.formula.functions.T;

import java.util.*;


/**
 * Flowers bloom all over the ground, frequent urination urination not enough!
 * IDE: Created with by IntelliJ IDEA.
 * program: yzcrm
 *
 * @author :jones
 * github: https://github.com/luckyrxp
 * email: jonesxr@163.com
 * version: v 1.0
 *
 * <p>
 * <p>
 * date: 2020-08-19
 * time: 16:50
 * description:
 * Copyright(©) 2020 by Jones
 **/

public class CrmQuotationItemService {

    /**
     * 报价项 : 报价模板 list
     * type 报价模块从 730 开始
     */
    public R filterConditionAndGetPageList() {
        List<Record> list = Db.find("SELECT * FROM `yzcrm_crm_quotation_item` WHERE del_flog = 1");
        return R.ok().put("data", list);
    }

    /**
     * 查询报价模板(字典表)
     */
    public R queryQuotationTemplate() {
        List<Record> optionList = Db.find(QuotationCombEnum.FIND_QUOTATION_ITEM.getName());
        List<Record> deployList = Db.find(QuotationCombEnum.FIND_DEPLOY_MODE.getName());
        List<Record> rateList = Db.find(QuotationCombEnum.FIND_VAT_RATE.getName());
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("quotation_item", optionList);
        resultMap.put("deploy_mode", deployList);
        resultMap.put("vat_rate", rateList);
        return R.ok().put("data", resultMap);
    }

    /**
     * 报价项 :新增/编辑报价模板
     */
    @Before(TxSerializable.class)
    public Object saveOrUpdate(BasePageRequest<T> basePageRequest) {

        JSONObject jsonObject = basePageRequest.getJsonObject();
        Record record = new Record();
        Log.getLog(getClass()).info(jsonObject.toString());

        CrmQuotationItem item = new CrmQuotationItem();
        Date date = new Date();
        item.setItemOption(jsonObject.getInteger("itemOption"));
        item.setPublicDeploy(jsonObject.getInteger("publicDeploy"));
        item.setLocalDeploy(jsonObject.getInteger("localDeploy"));
        item.setRemark(jsonObject.getString("remark"));

        boolean flag = false;

        if (jsonObject.getInteger("quotationItemId") == QuotationCombEnum.ADD.getCode()) {
            item.setRate(QuotationCombEnum.RATE.getCode());
            item.setCreateTime(date);
            item.setUpdateTime(date);
            item.setCreateUserId(Math.toIntExact(BaseUtil.getUser().getUserId()));
            item.setUpdateUserId(Math.toIntExact(BaseUtil.getUser().getUserId()));
            Record recordItem = record.setColumns(item);
            flag = Db.save("yzcrm_crm_quotation_item", recordItem);
        } else if (jsonObject.getInteger("quotationItemId") > QuotationCombEnum.ADD.getCode()) {
            item.setQuotationItemId(jsonObject.getInteger("quotationItemId"));
            item.setRate(jsonObject.getInteger("rate"));
            item.setUpdateTime(date);
            item.setUpdateUserId(Math.toIntExact(BaseUtil.getUser().getUserId()));
            Record recordItem = record.setColumns(item);
            flag = Db.update("yzcrm_crm_quotation_item", "quotation_item_id", recordItem);
        }
        return flag ? R.ok() : R.error();
    }

    /**
     * 删除报价模板
     *
     * @param basePageRequest basePageRequest
     */
    @Before(TxSerializable.class)
    public R del(BasePageRequest<T> basePageRequest) {
        Integer quotationItemId = basePageRequest.getJsonObject().getInteger("quotationItemId");
        if (quotationItemId == 0) {
            return R.error("模板id不正确,请重新确认!");
        }
        String permissions = "crm:quotationComb:delete";
        if (!BaseUtil.getUserId().equals(BaseConstant.SUPER_ADMIN_USER_ID) && !BaseUtil.getUser().getRoles().contains(BaseConstant.SUPER_ADMIN_ROLE_ID)
                && !AuthUtil.isHavaAuth(permissions)
                && Db.queryInt(Db.getSql("crm.quotation.queryIsOwnerUser"), BaseUtil.getUserId(), quotationItemId) <= 0) {
            return R.error("没有权限");
        }
        Record record = new Record();
        record.set("quotation_item_id", quotationItemId);
        record.set("del_flog", 2);
        boolean flag = Db.update("yzcrm_crm_quotation_item", "quotation_item_id", record);
        return flag ? R.ok() : R.error();
    }
}
